Generate list of applied templates in active DGN file with their start, end chainages, drop interval in tabular format
The below code iterates all the corridors with the applied template drops. It generates a report in tabular format with information template name, start chainage, end chainage, and drop interval.
//Required References
using System;
using Bentley.GeometryNET;
using Bentley.CifNET.GeometryModel.SDK;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using Bentley.DgnPlatformNET;
using Bentley.MstnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.CifNET.LinearGeometry;
using Bentley.CifNET.Formatting;
public void GenerateListOfAppliedTemplateInTabularFormat()
{
try
{
//Get active dgn, model and connection object
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
DgnFile dgnFile = dgnModel.GetDgnFile();
Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit con = Bentley.CifNET.SDK.Edit.ConsensusConnectionEdit.GetActive();
GeometricModel geomModel = con.GetActiveGeometricModel();
if (geomModel == null) return;
int rowCount = 0;
DPoint3d origin = new DPoint3d(0, 0, 0);
//Calculate the row count for table and calculate point for table origin
foreach (Corridor corridor in geomModel.Corridors)
{
if (rowCount == 0)
{
Alignment alignment = corridor.CorridorAlignment;
double length = alignment.LinearGeometry.Length / 2;
LinearPoint originPoint = alignment.LinearGeometry.GetPointAtDistanceOffset(length, 100);
//User need to convert this origin value to current units based on settings
double UorPerMeter = FormatSettingsConstants.GetUorPerMeter(Session.Instance.GetActiveDgnModel());
origin = new DPoint3d(originPoint.Coordinates.X * UorPerMeter, originPoint.Coordinates.Y * UorPerMeter, originPoint.Coordinates.Z * UorPerMeter);
}
rowCount += corridor.TemplateDrops.Count();
}
//Obtain DgnTextStyle to be applied to TextTable
DgnTextStyle dgnTextStyle = GetTableTextStyle();
BentleyStatus status = dgnTextStyle.GetProperty(TextStyleProperty.Height, out double textHeight);
//set row and column count for table
uint nRowCount = Convert.ToUInt32(rowCount);
uint nColumnCount = 4;
//Create TextTable Element and set it's Origin
TextTable templateDropTextTable = TextTable.Create(nRowCount, nColumnCount, dgnTextStyle.Id, textHeight, dgnModel);
templateDropTextTable.Origin = origin;
//set rotation value for table
DVector3d rotVector = new DVector3d(new DPoint3d(0, 0, 0));
DMatrix3d rotMatrix = DMatrix3d.Rotation(2, rotVector.AngleXY);
templateDropTextTable.Rotation = rotMatrix;
uint nRowId = 0;
//Iterate over the corridors and applied template drops and create text table cell with data to show
foreach (Corridor corridor in geomModel.Corridors)
{
IEnumerable<TemplateDrop> templateDrops = corridor.TemplateDrops;
foreach (TemplateDrop templateDrop in templateDrops)
{
//Genrate data to fill into table
string templateName = templateDrop.Template.TemplatePath;
string startDistance = templateDrop.StartDistance.ToString();
string endDistance = templateDrop.EndDistance.ToString();
string dropInterval = templateDrop.Interval.ToString();
//Fill TextTableCell with respective TextBlock
uint nColumnId = 0;
TextBlock txtBlock = new TextBlock(dgnTextStyle, dgnModel);
TextTableCell textTableCell = templateDropTextTable.GetCell(new TableCellIndex(nRowId, nColumnId));
textTableCell.TextBlock = txtBlock;
TextBlock templateNameTextBlock = textTableCell.TextBlock;
templateNameTextBlock.AppendText(templateName);
textTableCell.TextBlock = templateNameTextBlock;
nColumnId++;
textTableCell = templateDropTextTable.GetCell(new TableCellIndex(nRowId, nColumnId));
textTableCell.TextBlock = txtBlock;
TextBlock startDistanceTextBlock = textTableCell.TextBlock;
startDistanceTextBlock.AppendText(startDistance);
textTableCell.TextBlock = startDistanceTextBlock;
nColumnId++;
textTableCell = templateDropTextTable.GetCell(new TableCellIndex(nRowId, nColumnId));
textTableCell.TextBlock = txtBlock;
TextBlock endDistanceTextBlock = textTableCell.TextBlock;
endDistanceTextBlock.AppendText(endDistance);
textTableCell.TextBlock = endDistanceTextBlock;
nColumnId++;
textTableCell = templateDropTextTable.GetCell(new TableCellIndex(nRowId, nColumnId));
textTableCell.TextBlock = txtBlock;
TextBlock dropIntervalTextBlock = textTableCell.TextBlock;
dropIntervalTextBlock.AppendText(dropInterval);
textTableCell.TextBlock = dropIntervalTextBlock;
nColumnId++;
txtBlock.Dispose();
nRowId++;
}
}
//Set TextTableColumn Width from its contents
for (uint i = 0; i < nColumnCount; ++i)
{
TextTableColumn textTableColumn = templateDropTextTable.GetColumn(i);
textTableColumn.SetWidthFromContents();
}
//Color and LineStyle are applied as per active attributes
int color = Settings.Color;
int lineStyle = Settings.LineStyle;
int lineWeight = Settings.LineWeight;
templateDropTextTable.DefaultLineColor = (uint)color;
templateDropTextTable.DefaultLineStyle = lineStyle;
templateDropTextTable.DefaultLineWeight = (uint)lineWeight;
//Create TextTableElement and add to model
TextTableElement curveTextTableElement = new TextTableElement(templateDropTextTable, dgnModel);
ElementPropertiesSetter elementPropertiesSetter = new ElementPropertiesSetter();
elementPropertiesSetter.Apply(curveTextTableElement);
curveTextTableElement.AddToModel();
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
//Set active textstyle
protected DgnTextStyle GetTableTextStyle()
{
DgnTextStyle textStyle = null;
string activeTextStyleName = "";
try
{
DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
DgnFile dgnFile = dgnModel.GetDgnFile();
double dWidth = 0.00050;
double dHeight = 0.00050;
double dLineSpacing = 0.70;
string fontName = "Engineering Regular";
activeTextStyleName = "LM_Text";
Settings.TextWidth = dWidth;
Settings.TextHeight = dHeight;
Settings.LineSpacing = dLineSpacing;
Settings.Font = fontName;
Settings.TextStyleName = activeTextStyleName;
Settings.TextJustification = TextElementJustification.CenterMiddle;
textStyle = new DgnTextStyle(activeTextStyleName, dgnFile);
DgnFont dgnFont = dgnFile.GetDgnFontMap().FindFontByName(out uint foundNum, fontName, DgnFontFilterFlags.All);
textStyle.SetFontProperty(TextStyleProperty.Font, dgnFont);
textStyle.SetProperty(TextStyleProperty.Height, dHeight);
textStyle.SetProperty(TextStyleProperty.Width, dWidth);
textStyle.SetProperty(TextStyleProperty.LineSpacing, dLineSpacing);
textStyle.Add(dgnFile);
dgnModel.SaveModelSettings();
textStyle = DgnTextStyle.GetSettings(dgnFile);
Session.Instance.Keyin("textstyle active " + activeTextStyleName);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
return textStyle;
}
Output